<?php

class Modules {

    public static function showArticleListByCategoryID($categoryID, $showing) {

        $modules = new Modules();

        // tải chi tiết nhóm tin
        $category = $modules->getCategoryByID($categoryID);

        // danh sách bài viết thuộc nhóm tin
        $rows = $modules->getArticleListByCategoryID($categoryID);

        // tổng số tin 
        $amountOfArticle = count($rows);

        if ($amountOfArticle > 0) {


            if ($showing == 'block') {

                $xtemplate = new XTemplate('block/block_articles_category/block.html');

                $xtemplate->assign('CATEGORY_TITLE', $category->getTitle());

                for ($i = 0; $i < $amountOfArticle; $i++) {
                    if ($rows[$i]->getStatus() == 1) {

                        if ($i == 0) {
                            $xtemplate->assign('ARTICLE_ID', $rows[$i]->getID());
                            $xtemplate->assign('ARTICLE_IMAGE', $rows[$i]->getImage());
                            $xtemplate->assign('ARTICLE_ALIAS', $rows[$i]->getAlias());
                            $xtemplate->assign('ARTICLE_TITLE', $rows[$i]->getTitle());
                            $xtemplate->assign('ARTICLE_DESCRIPTION', Utility::subString($rows[$i]->getDescription(), 500, true));
                            $xtemplate->parse("main.large_block");
                        } else {

                            if ($i < 3) {
                                $xtemplate->assign('ARTICLE_ID', $rows[$i]->getID());
                                $xtemplate->assign('ARTICLE_IMAGE', $rows[$i]->getImage());
                                $xtemplate->assign('ARTICLE_ALIAS', $rows[$i]->getAlias());
                                $xtemplate->assign('ARTICLE_TITLE', $rows[$i]->getTitle());
                                $xtemplate->parse("main.above_small_block");
                            } else {
                                $xtemplate->assign('ARTICLE_ID', $rows[$i]->getID());
                                $xtemplate->assign('ARTICLE_IMAGE', $rows[$i]->getImage());
                                $xtemplate->assign('ARTICLE_ALIAS', $rows[$i]->getAlias());
                                $xtemplate->assign('ARTICLE_TITLE', $rows[$i]->getTitle());
                                $xtemplate->parse("main.under_small_block");
                            }
                        }
                    }
                }
            } else {

                $xtemplate = new XTemplate('block/block_articles_category/list.html');
                // số lượng hiển thị là 10
                $amountToView = 10;

                $xtemplate->assign('CATEGORY_TITLE', $category->getTitle());


                // nếu số lượng tin lấy được nhỏ hơn số lượng hiển thị
                // thì gán số lượng hiển thị bằng tổng số lấy được
                if ($amountOfArticle < $amountToView) {
                    $amountToView = $amountOfArticle;
                }


                for ($i = 0; $i < $amountToView; $i++) {
                    if ($rows[$i]->getStatus() == 1) {
                        $xtemplate->assign('ARTICLE_ID', $rows[$i]->getID());
                        $xtemplate->assign('ARTICLE_IMAGE', $rows[$i]->getImage());
                        $xtemplate->assign('ARTICLE_ALIAS', $rows[$i]->getAlias());
                        $xtemplate->assign('ARTICLE_TITLE', $rows[$i]->getTitle());
                        $xtemplate->assign('ARTICLE_DESCRIPTION', $rows[$i]->getDescription());
                        $xtemplate->assign('ARTICLE_DATE', Utility::formatDateTimeToView($rows[$i]->getModifiedDate()));
                        $xtemplate->parse("main.article");
                    }
                }
            }



            $xtemplate->parse("main");
            $xtemplate->out("main");
        }
    }

    public static function showVideoList() {

        $modules = new Modules();
        // get video list
        $videos = $modules->getListVideo();

        // amount of video
        $amountVideo = count($videos);

        if ($amountVideo > 0) {

            // xtemplate file
            $xtemplate = new XTemplate('block/block_video/index.html');

            // loop each video
            for ($i = 0; $i < $amountVideo; $i++) {

                $filePath = $videos[$i]->getFilePath();

                if ($videos[$i]->getFromYouTube() == 1) {
                    $filePath = substr($filePath, strpos($filePath, '?v=') + 3, strlen($filePath));
                }
                if ($i == 0) {
                    $xtemplate->assign("FIRST_FILE_PATH", $filePath);
                    $xtemplate->assign("FIRST_IMAGE", $videos[$i]->getImage());
                    $xtemplate->assign("FIRST_FROM_YOUTUBE", $videos[$i]->getFromYouTube());
                }

                $xtemplate->assign("IMAGE", $videos[$i]->getImage());
                $xtemplate->assign("FILE_PATH", $filePath);
                $xtemplate->assign("TITLE", $videos[$i]->getTitle());
                $xtemplate->assign("FROM_YOUTUBE", $videos[$i]->getFromYouTube());
                $xtemplate->parse("main.video_link");
            }
            $xtemplate->parse("main");
            $xtemplate->out("main");
        }
    }

    public static function getHorizontalMenu($categoryID, $parentID = 0) {
        $module = new Modules();

        $menuString = '';
        $result = $module->getMenuListByCategoryID($categoryID, $parentID);

        if ($result != null) {

            for ($i = 0; $i < count($result); $i++) {
                if ($result[$i]->getStatus() == 1) {
                    $menuString .='<li><a target="' . $result[$i]->getTarget() . '" href = "' . $result[$i]->getLink() . '">' . $result[$i]->getTitle() . '</a>';
                    $menuString .='<ul>';
                    $menuString .= $module->getHorizontalMenu($categoryID, $result[$i]->getID(), $menuString);
                    $menuString.='</ul>';
                    $menuString .='</li>';
                }
            }
        }
        return $menuString;
    }

    public static function showImageByCategoryID($categoryID) {

        $module = new Modules();

        // tải chi tiết nhóm
        $category = $module->getCategoryByID($categoryID);

        // tải danh sách ảnh
        $images = $module->getListImageByGalleryID($categoryID);


        if (count($images) > 0) {

            $xtemplate = new XTemplate('block/block_album/album_slideshow.html');

            $xtemplate->assign("CATEGORY_TITLE", $category->getTitle());
            $xtemplate->assign("CATEGORY_ID", $categoryID);

            for ($i = 0; $i < count($images); $i++) {
                $xtemplate->assign("IMAGE_PATH", $images[$i]->getPath());
                $xtemplate->assign("IMAGE_TITLE", $images[$i]->getTitle());
                $xtemplate->assign("IMAGE_LINK", $images[$i]->getDescription());
                $xtemplate->parse("main.image");
            }
            $xtemplate->parse("main");
            $xtemplate->out("main");
        }
    }

    public static function showHighLightArticleList() {

        $module = new Modules();

        // tải danh sách bài viết
        $rows = $module->getHighLightArticleList();

        if (count($rows) > 0) {
            $xtemplate = new XTemplate('block/block_highlight_articles/block.html');
            for ($i = 0; $i < count($rows); $i++) {
                if ($i == 0) {
                    $xtemplate->assign('ARTICLE_ID', $rows[$i]->getID());
                    $xtemplate->assign('ARTICLE_IMAGE', $rows[$i]->getImage());
                    $xtemplate->assign('ARTICLE_ALIAS', $rows[$i]->getAlias());
                    $xtemplate->assign('ARTICLE_TITLE', $rows[$i]->getTitle());
                    $xtemplate->assign('ARTICLE_DESCRIPTION', $rows[$i]->getDescription());
                    $xtemplate->parse("main.large_block");
                } else {
                    $xtemplate->assign('ARTICLE_ID', $rows[$i]->getID());
                    $xtemplate->assign('ARTICLE_IMAGE', $rows[$i]->getImage());
                    $xtemplate->assign('ARTICLE_ALIAS', $rows[$i]->getAlias());
                    $xtemplate->assign('ARTICLE_TITLE', $rows[$i]->getTitle());
                    $xtemplate->parse("main.small_block");
                }
            }
            $xtemplate->parse("main");
            $xtemplate->out("main");
        }
    }

    private function getCategoryByID($categoryID) {
        $db = new Database();
        try {
            $category = new Categories();
            $category->setID($categoryID);
            $rows = $category->getByID($db);
            $db->submitChanges();
            return $rows;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    
    
    

    
    private function getArticleListByCategoryID($categoryID) {
        $db = new Database();
        try {
            $article = new Articles();
            $article->setCategoryID($categoryID);
            $article->setStatus(1);
            $article->setValid(1);
            $article->setStartRow(0);
            $article->setLinePerPage(5);
            $rows = $article->getListByCategoryID($db);
            $db->submitChanges();
            return $rows;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    // hàm tải danh sách video
    private function getListVideo() {
        $db = new Database();
        try {

            $video = new Videos();
            $videos = $video->getList($db);
            $db->submitChanges();
            return $videos;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getMenuListByCategoryID($categoryID, $parentID = 0) {
        try {
            $db = new Database();
            $menu = new Menu();
            $menu->setCategoryID($categoryID);
            $menu->setParentID($parentID);
            $result = $menu->getListByParentIDAndCategoryID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getListImageByGalleryID($categoryID) {
        $db = new Database();
        try {
            $gallery = new Images();
            $gallery->setCategoryID($categoryID);
            $rows = $gallery->getListByCategoryID($db);
            return $rows;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            exit();
        }
    }

    private function getHighLightArticleList() {
        $db = new Database();
        try {
            $article = new Articles();
            $article->setHighLight(1);
            $article->setStatus(1);
            $article->setStartRow(0);
            $article->setLinePerPage(5);
            $article->setValid(1);
            $rows = $article->getOptiontList($db);
            $db->submitChanges();
            return $rows;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}
